package xzy.Controller;

import lombok.SneakyThrows;
import net.sf.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xzy.dao.StudentMesMapper;
import xzy.pojo.StudentMes;
import xzy.util.DateUtil;
import xzy.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "studentSave_Servlet")
public class studentSave_Servlet extends HttpServlet {
    private static ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
    StudentMesMapper studentMesMapper= (StudentMesMapper) context.getBean("studentMesMapper");
    StudentMes studentMes=new StudentMes();
    @SneakyThrows
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("修改学生信息");
        request.setCharacterEncoding("utf-8");
        String stuNo=request.getParameter("stuNo");
        String stuName=request.getParameter("stuName");
        String sex=request.getParameter("sex");
        String birthday=request.getParameter("birthday");
        String gradeId=request.getParameter("gradeId");
        String email=request.getParameter("email");
        String tel=request.getParameter("tel");
        String stuAddress=request.getParameter("stuAddress");
        String stuGradute=request.getParameter("stuGradute");
        String stuDesc=request.getParameter("stuDesc");
        String stuId=request.getParameter("stuId");
        studentMes=new StudentMes(Integer.parseInt(stuId),stuNo,stuName,sex,DateUtil.formatString(birthday,"yyyy-MM-dd")
                ,Integer.parseInt(gradeId),email,tel,stuAddress,stuGradute,stuDesc);
        int result=1;
        try{
            int res=studentMesMapper.UpdateStudentMes(studentMes);
            if(res>0){
                result=1;
            }else{
                result=0;
            }
        }catch(Exception e){
            result=0;
        }finally {
            response.getWriter().println(result);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
